package com.ytjj.qmyx.mall.runner;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.SensitiveWordUtil;
import com.ytjj.qmyx.mall.constants.RedisConstants;
import com.ytjj.qmyx.mall.mapper.*;
import com.ytjj.qmyx.mall.model.*;
import com.ytjj.qmyx.mall.model.vo.ChannelVO;
import com.ytjj.qmyx.mall.service.adapay.AdaPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 缓存启动加载类
 *
 * 
 * @version 1.0
 *
 */
@Component
@Slf4j
public class CacheRunner implements ApplicationRunner {

    @Resource
    private ImgMapper imgMapper;

    @Resource
    private UsersFakeMapper usersFakeMapper;

    private final RedisService redisService;

    @Resource
    private PbMapper pbMapper;

    @Resource
    private SysConfigMapper sysConfigMapper;

    @Resource
    private HotSearchMapper hotSearchMapper;

    @Resource
    private OrdersCommentMapper ordersCommentMapper;

    @Resource
    private EnvelopeConfigMapper envelopeConfigMapper;

    @Resource
    private CopywriterConfigMapper copywriterConfigMapper;

    @Resource
    private ChannelDao channelDao;

    @Resource
    private AdaPayService adaPayService;

    public CacheRunner(RedisService redisService) {
        this.redisService = redisService;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            /**
             * 图库
             */
            List<Img> list = imgMapper.selectByExample(new ImgExample());
            redisService.delete(RedisConstants.REDIS_KEY_IMG);
            list.stream().forEach(item ->
                    redisService.leftPush(RedisConstants.REDIS_KEY_IMG, JSONObject.toJSONString(item)));

            /**
             * 敏感内容
             */
            List<Pb> pbList = pbMapper.selectByExample(new PbExample());
            redisService.delete(RedisConstants.PD_KEY);
            pbList.stream().forEach(item ->
                    redisService.leftPush(RedisConstants.PD_KEY, JSONObject.toJSONString(item)));
            SensitiveWordUtil.init(new HashSet(pbList.stream().map(Pb::getContent).collect(Collectors.toList())));

            /**
             * 假用户
             */
            List<UsersFake> fakeUserList = usersFakeMapper.selectByExample(new UsersFakeExample());
            redisService.delete(RedisConstants.FAKE_USER);
            fakeUserList.stream().forEach(item ->
                    redisService.leftPush(RedisConstants.FAKE_USER, JSONObject.toJSONString(item)));


            /**
             * 获取系统配置
             */
//            List<SysConfig> sysConfigs = sysConfigMapper.selectByExample(new SysConfigExample());
//            redisService.delete(RedisConstants.SYS_CONFIG);
//            sysConfigs.stream().forEach(item -> {
//                if(!StringUtils.isBlank(item.getParamKey())){
//                    redisService.add(RedisConstants.SYS_CONFIG, item.getParamKey(), null == item.getParamValue() ? "" : item.getParamValue());
//                }
//            });

            /**
             * @Description 获取我的钱包 - 红包数额配置第一天
             *
             * 
             *
             * @Version 1.0
             **/
            EnvelopeConfigExample former = new EnvelopeConfigExample();
            former.createCriteria()
                    .andIdBetween(1, 4);
            List<EnvelopeConfig> formerEnvelopeConfigList = envelopeConfigMapper.selectByExample(former);
            redisService.delete(RedisConstants.RED_ENVELOPE_CONFIG_FORMER);
            formerEnvelopeConfigList.stream().forEach(item -> {
                redisService.leftPush(RedisConstants.RED_ENVELOPE_CONFIG_FORMER, JSONObject.toJSONString(item));
            });

            /**
             * @Description 获取我的钱包 - 红包数额配置- 非第一天
             *
             * 
             *
             * @Version 1.0
             **/
            EnvelopeConfigExample latter = new EnvelopeConfigExample();
            latter.createCriteria()
                    .andIdBetween(5, 8);
            List<EnvelopeConfig> envelopeConfigList = envelopeConfigMapper.selectByExample(latter);
            redisService.delete(RedisConstants.RED_ENVELOPE_CONFIG_LATTER);
            envelopeConfigList.stream().forEach(item -> {
                redisService.leftPush(RedisConstants.RED_ENVELOPE_CONFIG_LATTER, JSONObject.toJSONString(item));
            });

            /**
             * 热搜索词
             */
            List<HotSearch> searchList = hotSearchMapper.selectByExample(new HotSearchExample());
            redisService.delete(RedisConstants.HOT_SEARCH);
            searchList.stream().forEach(item ->
                    redisService.leftPush(RedisConstants.HOT_SEARCH, JSONObject.toJSONString(item)));


            List<OrdersComment> ordersCommentList = ordersCommentMapper.selectByExample(new OrdersCommentExample());
            redisService.delete(RedisConstants.ORDERS_COMMENT);
            ordersCommentList.stream().forEach(item ->
                    redisService.leftPush(RedisConstants.ORDERS_COMMENT, JSONObject.toJSONString(item)));


            /**
             * 文案配置
             */
            List<CopywriterConfig> copywriterConfigList = copywriterConfigMapper.selectByExample(new CopywriterConfigExample());
            redisService.delete(RedisConstants.COPYWRITER_CONFIG);
            copywriterConfigList.stream().forEach(item -> {
                redisService.add(RedisConstants.COPYWRITER_CONFIG, item.getParamKey(), item.getParamValue());
            });

            /**
             * 渠道跳转配置
             */
            List<ChannelVO> channels = channelDao.findChannelList();
            redisService.delete(RedisConstants.CHANNEL);
            channels.stream().forEach(item -> {
                redisService.add(RedisConstants.CHANNEL, item.getChannel(), item.getRedirectUrl());
            });

            /**
             * 初始化Adapay 汇付天下
             *
             */
            adaPayService.initAdapayConfig();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
